/*
 * PopupLibTypes.cpp
 *
 *  Created on: Jul 2, 2012
 *      Author: guillou
 */

#include <PopupLibTypes.hpp>

using namespace std;
using namespace Popup;

const UserList EmptyUserList;
const User     NullUser;

void User::update(const User & p_user,
                      unsigned short p_updateMask)
{
  id = p_user.getID();

  if (p_updateMask & POPUP_USER_FIELD_NICKNAME) {
    nickname = p_user.getNickname();
  }
  if (p_updateMask & POPUP_USER_FIELD_AVATAR) {
    avatar = p_user.getAvatar();
  }
  if (p_updateMask & POPUP_USER_FIELD_MODE) {
    mode = p_user.getMode();
  }
  if (p_updateMask & POPUP_USER_FIELD_HEALTH) {
    alive = p_user.isAlive();
  }
}

unsigned short UserStatistics::itemsMask() const {
  unsigned short _mask = 0x0000;
  const_iterator _it;
  for (_it = begin(); _it != end(); _it++) {
    _mask |= _it->first;
  }
  return _mask;
}

bool UserStatistics::isRecordman(UserRateItem p_item) const {
  return ((recordMask & p_item) != 0);
}

void UserStatistics::set(UserRateItem p_item, unsigned int p_value,
                         bool p_isRecord) {
  (*this)[p_item] = p_value;
  if (p_isRecord) {
    recordMask |= p_item;
  } else {
    recordMask &= ~p_item;
  }
}

bool UserStatistics::get(UserRateItem p_item, unsigned int & p_value,
                         bool & p_isRecord) const {
  const_iterator _it = find(p_item);
  if (_it != end()) {
    p_value = _it->second;
    p_isRecord = ((recordMask & p_item) != 0);
    return true;
  } else {
    return false;
  }
}

void strUserRateItem(UserRateItem p_item, string & p_string) {
  switch (p_item) {
    case POPUP_RATE_LIKE:
      p_string = "like"; break;
    case POPUP_RATE_DONTLIKE:
      p_string = "dontlike"; break;
    case POPUP_RATE_POETRY:
      p_string = "poetry"; break;
    case POPUP_RATE_FUCKYOU:
      p_string = "fuckyou"; break;
    default:
      p_string = "unknown"; break;
  }
}

void UserStatistics::dump() const {
  string _itemname;
  const_iterator _it;
  for (_it = begin(); _it != end(); _it++) {
    strUserRateItem(_it->first, _itemname);
    printf("stats[%s] = %u %s\n", _itemname.c_str(), _it->second,
           (((recordMask & _it->first) != 0? "*** record! ***" : "")));
  }
}
